/*****************************************************************************//*!
* file  : key.c
*******************************************************************************/

#include "mytype.h"
#include "gui_main.h"
#include "pincfg.h"
#include "os_obj.h"
#include "systask.h"


void lcm_light_tog(void);

key_click_type click_key;

volatile uint8_t click_key_cnt;
volatile uint8_t click_found_cnt;
volatile uint8_t click_lost_cnt;

/* key read
* 
*/
void key_scan(void)
{
    static char key_scnt = 0;
    uint16_t key_read;
    
    key_scnt++;
    if(key_scnt < 5)
        return;
    
    key_scnt = 0;
    
    key_read = (~((KEY1<<0)|(KEY2<<1)|(KEY3<<2)|(KEY4<<3)|(KEY5<<4)|(KEY6<<5)|(KEY7<<6)|(KEY8<<7))) & 0xFF ;
    
    key_st_isr(&gui_st.key, 3, key_read);
}

/*****************************************************************************//*!
*
* @brief   storage task.
*     
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void KeyTask(void const *argument)
{ 
    sys_st.disflt.period_sv = 5;
    sys_st.disflt.active_sv = 2;
    sys_st.disflt.flt_sv = 2;
    
    while(1)
    {
        
        osDelay(ms_ticks(50));
        key_st_handle(&gui_st.key, 1);
        
        click_key.space_sv = 6;

        key_click_handle(&click_key, (0!=(gui_st.key.val&KEY_VAL_SET)) ,1);
        
 
        sys_st.disflt.flt_pulse = 1;
        sys_st.disflt.time_pulse = 1;
        discont_in_flt(&sys_st.disflt, (0!=(gui_st.key.val&KEY_VAL_SET)));
        
        
        if(click_key.multi_click_out)
        {
            click_key_cnt++;
        }
        if(click_key.edge_found)
        {
            click_found_cnt++;
        }
        if(click_key.edge_lost)
        {
            click_lost_cnt++;
        }
        
        

        switch(gui_st.key.found_edge)
        {
            case 0:
                break;
            case KEY_VAL_SET:
				gui_st.move_en = !gui_st.move_en;
                break;
            case KEY_VAL_UP:
				gui_st.move_mode = 0;
                break;
            case KEY_VAL_DN:
				gui_st.move_mode = 1;
                break;
            case KEY_VAL_RIGHT:
				gui_st.move_en = 1;
				gui_st.move_mode = 1;
                break;
			case KEY_VAL_LEFT:
				gui_st.move_en = 1;
				gui_st.move_mode = 0;
                break;
			case KEY_VAL_VIEW:
				gui_st.page_reg++;
				break;
			case KEY_VAL_HOME:
				lcm_light_tog();
				break;
            default:
                break;
        }
    }
}

